﻿package com.kingtone.rongji.tool;

import org.apache.http.entity.ByteArrayEntity;
import android.content.Context;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class RongJiRestClient
{
	// 获取首页图片API地址
	private static final String API_HOME_PICS = "/Service/StoreManage.svc/Banner/Get";
	// 获取收货地址
	private static final String API_GET_SHIPING_ADDRESS = "/Service/SalesManage.svc/UserShipingAddress/Get";
	// 删除收货地址
	private static final String API_DEL_SHIPING_ADDRESS = "/Service/SalesManage.svc/UserShipingAddress/Delete";
	// 新增收货地址
	private static final String API_CREATE_SHIPING_ADDRESS = "/Service/SalesManage.svc/UserShipingAddress/Create?appKey=" + AppRestClient.API_KEY;
	// 更新收货地址
	private static final String API_UPDATE_SHIPING_ADDRESS = "/Service/SalesManage.svc/UserShipingAddress/Update?appKey=" + AppRestClient.API_KEY;
	// 获取省市区信息
	private static final String API_GET_REGION = "/Service/StoreManage.svc/RegionBasicInfo/Get";
	// 获取分类
	private static final String API_CATEGORY = "/Service/ProductManage.svc/Category/All";

	// 获取未支付和未收货订单
	public static final String API_GET_NOT_RECEIVE_OR_NOT_PAY_ORDER = "/Service/SalesManage.svc/Order/GetUserRecentlyOrderByOrderStatus?appKey="
			+ AppRestClient.API_KEY;

	// 获取全部订单
	public static final String API_GET_USER_RECENTLY_ORDER = "/Service/SalesManage.svc/Order/GetRecentlyForUser?appKey=" + AppRestClient.API_KEY;

	// 生成订单
	private static final String API_CREATE_ORDER = "/Service/SalesManage.svc/Order/CreateByApp?appKey=" + AppRestClient.API_KEY;

	//取消订单
	private static final String API_CANCEL_ORDER = "/Service/SalesManage.svc/Order/VoidByApp?appKey=" + AppRestClient.API_KEY;
	
	private static final String API_PROMOTION_CODE = "/Service/SalesManage.svc/Order/GetPromotionCode?appKey=" + AppRestClient.API_KEY;
	
	// 获取产品列表
	private static final String API_PRODUCT_GET = "/Service/ProductManage.svc/Product/Get?appKey=" + AppRestClient.API_KEY;
	
	//获取地区信息
	private static final String API_GET_REGION_ALL = "/Service/StoreManage.svc/Regions";
	
	//计算订单费用
	private static final String API_ORDER_CALCULATE = "/Service/SalesManage.svc/Order/Calculate?appKey=" + AppRestClient.API_KEY;
	
	//获取订单详情
	private static final String API_GET_ORDER_DETAIL = "/Service/SalesManage.svc/OrderDetailInfo/Get";
	
	//获取收藏列表
	private static final String API_GET_FAVORITE_LIST = "/Service/Accounts.svc/Customer/ProductFavorite/Get?appKey=" + AppRestClient.API_KEY;
	
	//删除收藏
	private static final String API_GET_DEL_FAVORITE = "/Service/Accounts.svc/Customer/ProductFavorite/Delete";
	
	//获取评论列表
	private static final String API_GET_REVIEW_LIST = "/Service/SalesManage.svc/Order/GetUserReviewProductList?appKey=" + AppRestClient.API_KEY;
	
	//获取优惠卷列表
	private static final String API_PROMOTION_LIST = "/Service/SalesManage.svc/PrmotionCode/Get?appKey=" + AppRestClient.API_KEY;
	
	//鲜享卡或银信商通卡账号余额查询
	private static final String API_CARD_BALANCE = "/Service/SalesManage.svc/Yinxin/GetYinxinCardBalance?appKey=" + AppRestClient.API_KEY;
	
	//鲜享卡或银信商通卡折扣
	private static final String API_CARD_DISCOUNT = "/Service/SalesManage.svc/Yinxin/GetYinxinCardDiscount?appKey=" + AppRestClient.API_KEY;
	
	//鲜享卡或银信商通卡支付
	private static final String API_CARD_PAY = "/Service/SalesManage.svc/Yinxin/PayOrder?appKey=" + AppRestClient.API_KEY;
	
	//登录
	public static final String LOGIN_URL = "/Service/CommonServicer.svc/Users/Login";

	//注册
	public static final String REGISTER_URL = "/Service/CommonServicer.svc/Users/CreateOrUpdate?appKey=" + AppRestClient.API_KEY;
	
	//第三方注册
	public static final String REGISTER_THIRDPART_URL = "/Service/CommonServicer.svc/Users/CreateOrUpdateByWebSite?appKey=" + AppRestClient.API_KEY;

	//获得用户信息
	public static final String USER_INFO_URL = "/Service/CommonServicer.svc/Users/Get";
	
	//根据用户名获得用户信息
	public static final String USER_INFO_BY_NAME_URL = "/Service/CommonServicer.svc/User/Get";
	
	//修改密码
	public static final String USER_CHANG_PWD_URL = "/Service/CommonServicer.svc/Users/ChangePassword?appKey=" + AppRestClient.API_KEY;
	
	//对密码进行编码
	public static final String USER_ENCODE_PWD_URL = "/Service/CommonServicer.svc/Common/EncryptPassword?appKey="+ AppRestClient.API_KEY;
	
	//重置密码
	public static final String RESET_EMAIL_URL = "/Service/CommonServicer.svc/Users/ResetPassword";
	
	//获得短信验证码
	public static final String SMS_VERIFY_URL = "/Service/SystemTools.svc/SMSCode/Get";
	
	//验证短信验证码
	public static final String SMS_CHECK_URL = "/Service/SystemTools.svc/SMSCode/Check";
	
	//获得用户的订单数量
	public static final String USER_COUNTS_URL = "/Service/SalesManage.svc/Order/GetUserOrderCount"; 
	
	//获得产品列表
	public static final String PRODUCTS_COMMON_URL = "/Service/ProductManage.svc/Product/Get?appKey=" + AppRestClient.API_KEY;
	
	//获得产品的图片
	public static final String PRODUCT_DETAIL_IMAGES_URL = "/Service/ProductManage.svc/Product/Image/Get";
	
	//获得产品详情
	public static final String PRODUCT_DETAIL_URL = "/Service/ProductManage.svc/Product/Detail";
	
	//获得产品分组详情
	public static final String PRODUCT_GROUP_URL = "/Service/ProductManage.svc/Product/ProductGroupProperty/Get/";
	
	//加入收藏
	public static final String PRODUCT_ADD_FAV_URL = "/Service/Accounts.svc/Customer/ProductFavorite/Create";
	
	//判断是否已经收藏
	public static final String PRODUCT_CHECK_FAV_URL = "/Service/Accounts.svc/Customer/ProductFavorite/CheckAlreadyFavorite";
	
	//获得分类菜单
	public static final String CATEGORY_ALL_URL = "/Service/ProductManage.svc/Category/All";

	// 获取预售产品
	public static void getYuShou(AsyncHttpResponseHandler responseHandler) {
		RequestParams params = new RequestParams();
		params.put("ProductType", "0");
		params.put("PromotionType", "89");
		params.put("IsWebsiteSearch", true);
		params.put("PageIndex", "0");
		params.put("PageSize", "50");
		params.setUseJsonStreamer(true);
		AppRestClient.post(API_PRODUCT_GET, params, responseHandler);
	}
	
	// 获取首页图片
	public static void getHomePics(String advCode, String advImageId, String themes, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("advCode", advCode);
		params.put("advImageId", advImageId);
		params.put("themes", themes);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(API_HOME_PICS, params, responseHandler);
	}

	// 获取分类
	public static void getCategory(String type, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("type", type);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(API_CATEGORY, params, responseHandler);
	}

	/**
	 * 获取收货地址
	 * 
	 * @param userId
	 * @param shipingId
	 * @param responseHandler
	 */
	public static void getShippingAddress(String userId, String shippingId, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("userId", userId);
		params.put("shippingId", shippingId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(API_GET_SHIPING_ADDRESS, params, responseHandler);
	}

	/**
	 * 删除收货地址
	 * 
	 * @param shipingId
	 * @param responseHandler
	 */
	public static void delShipingAddress(int shippingId, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("shippingId", shippingId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(API_DEL_SHIPING_ADDRESS, params, responseHandler);
	}

	/**
	 * 新增收货地址
	 * 
	 * @param userId
	 * @param cellPhone
	 * @param regisonId
	 * @param shipTo
	 * @param address
	 * @param zipCode
	 * @param responseHandler
	 */
	public static void createOrUpdateShipingAddress(String userId, String shipTo, String cellPhone, int regionId, String address, String zipCode,
			int shippingId, int type, int isDefault, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		String apiUrl = API_CREATE_SHIPING_ADDRESS;
		params.put("UserId", userId);
		params.put("ShipTo", shipTo);
		params.put("CellPhone", cellPhone);
		params.put("RegionId", regionId);
		params.put("Address", address);
		params.put("Zipcode", zipCode);
		params.put("IsDefault", isDefault);
		if (type == AppContasts.TO_MDOFY_ADDRESS)
		{
			params.put("ShippingId", shippingId);
			apiUrl = API_UPDATE_SHIPING_ADDRESS;
		}
		params.setUseJsonStreamer(true);
		AppRestClient.post(apiUrl, params, responseHandler);
	}

	/**
	 * 获取区域信息
	 * 
	 * @param parentId
	 * @param responseHandler
	 */
	public static void getRegion(int parentId, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("parentId", parentId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(API_GET_REGION, params, responseHandler);
	}

	/**
	 * 获取订单列表
	 * 
	 * @param userId
	 * @param pageIndex
	 * @param pageSize
	 * @param orderStatus
	 * @param responseHandler
	 */
	@SuppressWarnings("rawtypes")
	public static void getOrderList(Context context, String jsongStr, String url, BaseJsonHttpResponseHandler responseHandler)
	{
		ByteArrayEntity entity = new ByteArrayEntity(jsongStr.getBytes());
		entity.setContentType("application/json");
		AppRestClient.postEntity(context, url, entity,responseHandler);
	}

	/**
	 * 
	 * @param responseHandler
	 */
	public static void getRegionAll(AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(API_GET_REGION_ALL, params, responseHandler);
	}
	
	/**
	 * 计算订单费用
	 * @param params
	 * @param responseHandler
	 */
	@SuppressWarnings("rawtypes")
	public static void calculateOrder(Context context, String jsongStr, BaseJsonHttpResponseHandler responseHandler)
	{
		ByteArrayEntity entity = new ByteArrayEntity(jsongStr.getBytes());
		entity.setContentType("application/json");
		AppRestClient.postEntity(context, API_ORDER_CALCULATE, entity,responseHandler);
	}

	/**
	 * 创建订单
	 * @param params
	 * @param responseHandler
	 */
	@SuppressWarnings("rawtypes")
	public static void createOrder(Context context, String jsongStr, BaseJsonHttpResponseHandler responseHandler)
	{
		ByteArrayEntity entity = new ByteArrayEntity(jsongStr.getBytes());
		entity.setContentType("application/json");
		AppRestClient.postEntity(context, API_CREATE_ORDER, entity,responseHandler);
	}
	
	@SuppressWarnings("rawtypes")
	public static void getPromotionCode(Context context, String jsongStr, BaseJsonHttpResponseHandler responseHandler)
	{
		ByteArrayEntity entity = new ByteArrayEntity(jsongStr.getBytes());
		entity.setContentType("application/json");
		AppRestClient.postEntity(context, API_PROMOTION_CODE, entity,responseHandler);
	}
	
	/**
	 * 获取订单详情
	 * @param orderNumber
	 * @param responseHandler
	 */
	public static void getOrderDetail(String orderNumber, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("orderNumber", orderNumber);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(API_GET_ORDER_DETAIL, params, responseHandler);
	}
	
	/**
	 * 获取收藏列表
	 * @param userId
	 * @param responseHandler
	 */
	public static void getFavoriteList(String userId, int pageIndex, int pageSize, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("InUser", userId);
		params.put("PageIndex", pageIndex);
		params.put("PageSize", pageSize);
		params.setUseJsonStreamer(true);
		AppRestClient.post(API_GET_FAVORITE_LIST, params, responseHandler);
	}
	
	/**
	 * 删除收藏
	 * @param userId
	 * @param productId
	 * @param responseHandler
	 */
	public static void delFavorite(String userId, String productId, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("user", userId);
		params.put("productID", productId);
		params.put("appKey", AppRestClient.API_KEY);
		AppRestClient.get(API_GET_DEL_FAVORITE, params, responseHandler);
	}
	
	/**
	 * 获取评论列表
	 * @param userId
	 * @param pageIndex
	 * @param pageSize
	 * @param responseHandler
	 */
	public static void getReviewList(String userId, int pageIndex, int pageSize, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("InUser", userId);
		params.put("PageIndex", pageIndex);
		params.put("PageSize", pageSize);
		params.setUseJsonStreamer(true);
		AppRestClient.post(API_GET_REVIEW_LIST, params, responseHandler);
	}
	
	/**
	 * 获取优惠卷
	 * @param userId
	 * @param pageIndex
	 * @param pageSize
	 * @param status
	 * @param responseHandler
	 */
	public static void getPromotionList(String userId, int pageIndex, int pageSize, int status, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("UserID", 3098);
		params.put("PageIndex", pageIndex);
		params.put("PageSize", pageSize);
		params.put("Status", status);
		params.put("PromotionType",0);
		
		params.setUseJsonStreamer(true);
		AppRestClient.post(API_PROMOTION_LIST, params, responseHandler);
	}
	
	/**
	 * 鲜享卡或银信商通卡账号余额查询
	 * @param cardNo
	 * @param password
	 * @param responseHandler
	 */
	public static void getCardBalance(String cardNo, String password, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("CardNumber", cardNo);
		params.put("Password", password);
		
		params.setUseJsonStreamer(true);
		AppRestClient.post(API_CARD_BALANCE, params, responseHandler);
	}
	
	/**
	 * 获取联名卡折扣
	 * @param cardNo
	 * @param password
	 * @param responseHandler
	 */
	public static void getCardDiscount(String cardNo, String orderNo, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("CardNumber", cardNo);
		params.put("OrderNumber", orderNo);
		
		params.setUseJsonStreamer(true);
		AppRestClient.post(API_CARD_DISCOUNT, params, responseHandler);
	}
	
	/**
	 * 卡支付
	 * @param cardNo
	 * @param orderNo
	 * @param cardPwd
	 * @param responseHandler
	 */
	public static void cardPay(String cardNo,String orderNo, String cardPwd, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("CardNumber", cardNo);
		params.put("OrderNumber", orderNo);
		params.put("Password", cardPwd);
		params.setUseJsonStreamer(true);
		AppRestClient.post(API_CARD_PAY, params, responseHandler);
	}
	
	/**
	 * 取消订单
	 * @param userId
	 * @param orderNum
	 * @param reason
	 * @param resonType
	 * @param responseHandler
	 */
	public static void cancelOrder(String userId, String orderNum,String reason, int resonType, AsyncHttpResponseHandler responseHandler)
	{
		RequestParams params = new RequestParams();
		params.put("OrderNumber", orderNum);
		params.put("OrderStatus", 13);
		params.put("Type", 3);
		params.put("UserId", userId);
		params.put("VoidReason", reason);
		params.put("VoidReasonType", resonType);
		params.setUseJsonStreamer(true);
		AppRestClient.post(API_CANCEL_ORDER, params, responseHandler);
	}
	
}
